
package upcoming.client;

import java.util.*;
import java.text.*;

public class EventSearchCriteria implements java.io.Serializable
{
	private static final long serialVersionUID = 1L;

	static private final String MAX_DATE_FORMAT = "yyyy-MM-dd";
	static private final String MIN_DATE_FORMAT = "yyyy-MM-dd";
	
	private Double latitude;
	private Double longitude;
	private String location;
	private String search;
	private String placeId;
	private Integer radius;
	private boolean variety = false;
	private List<String> categoryIds = new ArrayList<String>();
	private Calendar minDate = null;
	private Calendar maxDate = null;
	private boolean best;
	private String whereOnEarthId;
	
	public void addCategory(String categoryId)
	{
		categoryIds.add(categoryId);
	}
	
	public void setLatLong(String latlong)
	{
		StringTokenizer tokenizer = new StringTokenizer(latlong, " \n\t,");
		while (tokenizer.hasMoreTokens())
		{
			setLatitude(Double.parseDouble(tokenizer.nextToken()));
			setLongitude(Double.parseDouble(tokenizer.nextToken()));
		}
		
	}
	
	public void setLatLong(double lat, double lon)
	{
		this.setLatitude(lat);
		this.setLongitude(lon);
	}
	
	public void setSearch(String s)
	{
		this.search = s;
	}


	public boolean hasLatLong()
	{
		return ((this.latitude != null) && (this.longitude != null));
	}
	
	public String getLatLong()
	{
		if (hasLatLong())
		{
			return this.latitude + ", " + this.longitude;
		}
		else
		{
			throw new IllegalStateException("no lat/long");
		}
	}
	
	public Double getLatitude()
	{
		return latitude;
	}

	public void setLatitude(Double latitude)
	{
		this.latitude = latitude;
	}

	public Double getLongitude()
	{
		return longitude;
	}

	public void setLongitude(Double longitude)
	{
		this.longitude = longitude;
	}

	public String getSearch()
	{
		return search;
	}

	public Map<String, String> toMap()
	{
		
		Map<String, String> m = new LinkedHashMap<String, String>();
		
		if (this.getBest())
		{
			m.put("method", "event.getBestInPlace");
		}
		else
		{
			m.put("method", "event.search");
		}
		
		if (this.getWhereOnEarthId() != null)
		{
			m.put("woeid", getWhereOnEarthId());
		}
		
		if (this.getSearch() != null)
		{
			m.put("search_text", this.getSearch());
		}
		
		if (hasLatLong())
		{
			m.put("location", "\"" + this.getLatLong() + "\"");
		}
		else if (this.location != null)
		{
			m.put("location", "\"" + location + "\"");
		}
		
		
		if (this.getRadius() != null)
		{
			m.put("radius", this.getRadius().toString());
		}
		
		if (getVariety())
		{
			m.put("variety", "1");
		}
		
		if (this.categoryIds.size() > 0)
		{
			m.put("category_id", Util.toString(categoryIds, ','));
		}
		
		if (this.placeId != null)
		{
			m.put("place_id", this.placeId);
		}
		
		SimpleDateFormat minFormat = new SimpleDateFormat(MIN_DATE_FORMAT);
		m.put("min_date", (minDate == null) ? minFormat.format(Calendar.getInstance().getTime()) : minFormat.format(minDate.getTime()));
		
		if (this.maxDate != null)
		{
			SimpleDateFormat maxFormat = new SimpleDateFormat(MAX_DATE_FORMAT);
			m.put("max_date", maxFormat.format(maxDate.getTime()));
		}
		
		return m;
		
	}

	/**
	 * 
	 * @return radius in miles
	 * 
	 */
	public Integer getRadius()
	{
		return radius;
	}

	
	/*
	 * 
	 *  radius in miles
	 *  
	 */
	public void setRadius(int searchRadius)
	{
		if (searchRadius < 0)
		{
			this.radius = null;
		}
		else
		{
			this.radius = new Integer(searchRadius);
		}
	}

	public boolean getVariety()
	{
		return variety;
	}

	public void setVariety(boolean b)
	{
		this.variety = b;
	}

	public void setLocation(String loc)
	{
		location = loc;
	}

	public void setPlaceId(String id)
	{
		this.placeId = id;
	}
	
	public void setMinDate(Calendar c)
	{
		minDate = c;
	}
	
	public void setMaxDate(Calendar c)
	{
		maxDate = c;
	}

	public void setWhereOnEarthId(String woeid)
	{
		this.whereOnEarthId = woeid;
	}

	public void setBest(boolean b)
	{
		this.best = b;
	}

	public boolean getBest()
	{
		return best;
	}

	public String getWhereOnEarthId()
	{
		return whereOnEarthId;
	}
	
	
}
